package cn.iocoder.yudao.module.crm.service.task;

import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.crm.controller.admin.task.vo.TaskIsApproverRespVO;
import cn.iocoder.yudao.module.crm.controller.admin.task.vo.taskapprover.TaskApproverReqVO;
import cn.iocoder.yudao.module.crm.dal.dataobject.task.TaskApproverDO;
import cn.iocoder.yudao.module.crm.dal.dataobject.tasktype.TaskTypeApproverDO;
import cn.iocoder.yudao.module.crm.dal.mysql.task.TaskApproverMapper;
import cn.iocoder.yudao.module.crm.dal.mysql.task.TaskMapper;
import cn.iocoder.yudao.module.crm.dal.mysql.tasktype.TaskTypeApproverMapper;
import cn.iocoder.yudao.module.crm.enums.task.CrmTaskApproverStatus;
import cn.iocoder.yudao.module.crm.enums.task.CrmTaskReviewStatus;
import cn.iocoder.yudao.module.crm.service.tasktype.TaskTypeApproverService;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

/**
 * CRM 任务单审批人关联 Service 实现类
 *
 * @author 广州氪量
 */
@Service
@Validated
public class TaskApproverServiceImpl implements TaskApproverService {
    @Resource
    private TaskApproverMapper taskApproverMapper;
    @Resource
    private TaskTypeApproverMapper taskTypeApproverMapper;
    @Resource
    private TaskMapper taskMapper;
    @Override
    public Map<Long, TaskIsApproverRespVO> getIsApproverByTaskIds(Set<Long> taskIds) {
        Map<Long,TaskIsApproverRespVO> taskIsApproverRespVOMap = new HashMap<>();
        // 1、获取当前登录用户编号
        Long loginUserId = getLoginUserId();
        // 2、遍历任务单ID，获取所有任务单审批信息
        for (Long taskId : taskIds) {
            Map<Integer, TaskApproverDO> collect = taskApproverMapper.selectListByTaskId(taskId).stream()
                    .collect(Collectors.toMap(TaskApproverDO::getReviewOrder, taskApproverDO -> taskApproverDO));
            //
            TaskIsApproverRespVO taskIsApproverRespVO = new TaskIsApproverRespVO();
            taskIsApproverRespVOMap.put(taskId, taskIsApproverRespVO);
        }
        return taskIsApproverRespVOMap;
    }

    @Override
    public void createTaskApprover(Long taskTypeId, Long taskId) {
        // 根据任务单类型ID，查询定义的审核流程
        List<TaskTypeApproverDO> taskTypeApprovers = taskTypeApproverMapper.selectListByTaskTypeId(taskTypeId);
        // 添加任务单审批人信息
        taskTypeApprovers.forEach(taskTypeApprover -> {
            TaskApproverDO taskApprover = new TaskApproverDO();
            taskApprover.setTaskId(taskId);
            taskApprover.setUserId(taskTypeApprover.getUserId());
            taskApprover.setReviewOrder(taskTypeApprover.getReviewOrder());
            taskApprover.setReviewStatus(CrmTaskReviewStatus.PEND_REVIEW.getStatus());
           taskApproverMapper.insert(taskApprover);
        });
    }

    @Override
    public void deleteTaskApprover(Long taskId) {
        taskApproverMapper.delete(new LambdaQueryWrapperX<TaskApproverDO>().eq(TaskApproverDO::getTaskId, taskId));
    }

    @Override
    public Long updateApprover(TaskApproverReqVO taskApproverReqVO) {
        TaskApproverDO taskApproverDO = new TaskApproverDO();
        BeanUtils.copyProperties(taskApproverReqVO, taskApproverDO);
        taskApproverDO.setUserId(getLoginUserId());
        taskApproverMapper.updateOne(taskApproverDO);
        return taskApproverReqVO.getTaskId();
    }
}